<?php

/**
 * Description of CommentModel
 * 评价数据表模型类
 * @author chacha
 * @date 2016-7-12
 */
class CommentModel extends BaseModel{
    //put your code here
    protected $tableName  = 'usercomment'; 
    protected $uid = 0;


    public function __construct($uid) {
        parent::__construct();
        $this->uid = $uid;
    }
    
    /**
     * 评价雇主or雇员
     * $item,数组，已包含ct,uid,author,content,rate1,rate2,rate3,rate4字段数据
     * 返回值:数组，包含状态码和错误提示
     */
    public function comment($item) {
        //判断是雇员还是雇主
        $ct = 0;
        $rsJob = M('job_user')->where("jobid={$item['job']} and uid=".$this->uid)->find(false);
        if ($rsJob) {
            $ct = \App\Common\Constvars::APPRAISE_TO_EMPLOYER;
        }
        else {
            $ct = \App\Common\Constvars::APPRAISE_TO_EMPLOYEE;
        }
        
        $now = time();
        
        $field = "ju.jobid,ju.jointime,j.name as jname";
        $job = M()->table(array('xia_job'=>'j', 'xia_job_user'=>'ju'))->field($field)->where("j.id={$item['job']} and j.state=".\App\Common\Constvars::STATUS_NORMAL." and ju.jobid=j.id")->find();
        if (!$job) {
            return $this->fail("无此任职记录");
        }
        
        $data = array(
            'ct'=>$ct,
            'startdate' => $job['jointime'],
            'endate' => $now,
            'status' => \App\Common\Constvars::STATUS_NORMAL,
        );
        $newitem = array_merge($item, $data);//合并所有字段

        $ret = M('usercomment')->add($newitem);
        if ($ret) {
            return $this->success();
        }
        else {
            return $this->fail("服务器内部错误");
        }
    }
    
    public function echoModel() {
        return "echo from CommentModel";
    }
}
